package com.controller.sys.sysLoginInfo;

import com.model.base.PageResult;
import com.model.base.Result;
import com.common.anntation.Log;
import com.common.emums.BusinessType;
import com.common.emums.DataStatus;

import com.common.excel.ExcelUtil;
import com.common.exception.BusinessException;
import com.common.utils.DateUtils;
import com.common.utils.EntityConversionUtil;
import com.core.springSecurity.service.PermissionService;
import com.model.dto.sys.sysLoginInfo.GetSysLoginInfoListDTO;
import com.model.dto.sys.sysLoginInfo.SysLoginInfoExcel;
import com.model.dto.sys.sysLoginInfo.UpdateSysLoginInfoStatusDTO;
import com.github.pagehelper.PageInfo;
import com.model.pojo.sys.SysLoginInfo;
import com.service.sys.SysLoginInfoService;
import com.model.vo.sys.sysLoginInfo.GetSysLoginInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author: zql
 * @Description: 登录日志模块
 * @Data: 2021-07-14
 */
@Api(tags = {"登录日志"})
@Slf4j
@RestController
@RequestMapping("/v1")
public class SysLoginInfoController {
    @Resource
    SysLoginInfoService sysLoginInfoService;

    /**
    * @Author: zql
    * @Description: 查看登录日志列表
     * @param getSysLoginInfoListDTO 请求数据
     * @Data: 2021-07-14
     */
    @ApiOperation(value = "查看登录日志列表")
    @PostMapping("/getSysLoginInfoList")
    @PreAuthorize("@ss.hasAnyPermi('" + PermissionService.SYSTEM_OPERATE + "')")
    public Result<PageResult<List<GetSysLoginInfoVO>>> getSysLoginInfoList(@RequestBody @Valid GetSysLoginInfoListDTO getSysLoginInfoListDTO) {
        List<SysLoginInfo> sysLoginInfoList = sysLoginInfoService.getList(getSysLoginInfoListDTO);
        PageInfo<SysLoginInfo> pageInfo = new PageInfo<>(sysLoginInfoList);
        List<GetSysLoginInfoVO> getSysLoginInfoVOList = EntityConversionUtil.copyList(sysLoginInfoList, GetSysLoginInfoVO.class);
        PageResult<List<GetSysLoginInfoVO>> build = PageResult.<List<GetSysLoginInfoVO>>builder().result(getSysLoginInfoVOList).pageInfo(pageInfo).build();
        return Result.suc(build);
    }

    /**
     * @Author: zql
     * @Description: 修改登录日志状态
     * @param updateSysLoginInfoStatusDTO 请求数据
     * @Data: 2021-07-14
     */
    @ApiOperation(value = "修改登录日志状态")
    @Log(title = "修改登录日志状态", businessType = BusinessType.UPDATE)
    @PostMapping("/updateSysLoginInfoStatus")
    @PreAuthorize("@ss.hasAnyPermi('" + PermissionService.SYSTEM_OPERATE + "')")
    public Result<Objects> updateSysLoginInfoStatus(@RequestBody @Valid UpdateSysLoginInfoStatusDTO updateSysLoginInfoStatusDTO) {
        SysLoginInfo sysLoginInfoDB = sysLoginInfoService.get(updateSysLoginInfoStatusDTO.getSysLoginInfoId(),null);
        if(DataStatus.OK.equals(updateSysLoginInfoStatusDTO.getIsDelete()) && !DataStatus.OK.equals(sysLoginInfoDB.getIsDelete())){
            log.debug("数据从停用/删除状态改成正常状态,需要做唯一性校验从这执行");
        }

        SysLoginInfo sysLoginInfo = new SysLoginInfo();
        sysLoginInfo.setSysLoginInfoId(updateSysLoginInfoStatusDTO.getSysLoginInfoId());
        sysLoginInfo.setIsDelete(updateSysLoginInfoStatusDTO.getIsDelete());
        sysLoginInfoService.update(sysLoginInfo);
        return Result.suc();
    }

   /**
     * @Author: zql
     * @Description: 导出登录日志表
     * @param getSysLoginInfoListDTO 请求数据
     * @param response 返回内容
     * @Data: 2021-07-14
     */
    @Log(title = "导出登录日志表", businessType = BusinessType.EXPORT, isSaveResponseData = false)
    @ApiOperation(value = "导出登录日志表")
    @PostMapping(value = "/exportSysLoginInfo")
    @PreAuthorize("@ss.hasAnyPermi('" + PermissionService.SYSTEM_OPERATE + "')")
    public Result<Objects> exportSysLoginInfo(@RequestBody @Valid GetSysLoginInfoListDTO getSysLoginInfoListDTO, HttpServletResponse response) throws BusinessException {
        List<SysLoginInfo> sysLoginInfoList = sysLoginInfoService.getList(getSysLoginInfoListDTO);
        List<SysLoginInfoExcel> sysLoginInfoExcelList = new ArrayList<>();
        for (SysLoginInfo sysLoginInfo : sysLoginInfoList) {
            SysLoginInfoExcel sysLoginInfoExcel = new SysLoginInfoExcel();
            BeanUtils.copyProperties(sysLoginInfo, sysLoginInfoExcel);
            sysLoginInfoExcel.setUserType(sysLoginInfo.getUserType().intValue());
            sysLoginInfoExcelList.add(sysLoginInfoExcel);
        }
        String fileName = "登录日志信息表" + DateUtils.parseDateToStr(DateUtils.YYYYMMDD_HHMM, new Date());
        try {
            ExcelUtil.writeExcel(response, sysLoginInfoExcelList, fileName, "登录日志信息", new SysLoginInfoExcel());
        } catch (IOException e) {
            log.error("导出失败，文件名:{}", fileName);
        }
        return Result.suc();
    }
}